<?php

namespace app\erp\controller;

use Curl\Curl;

class Token
{
    protected $client_id;
    protected $client_secret;
    protected $account_id;
    protected $user_name;
    protected $url                      = "http://113.140.31.86:8022/ierp/kapi/";


    //过期时长 秒
    protected $cache_access_token_time          = 6600;//6600;
    //刷新过期时长
    protected $cache_refresh_access_token_time  = 6800;
    //缓存key
    protected $cache_access_token_name;

    protected $cache_refresh_access_token_name;


    public function __construct($client_id,$client_secret,$account_id,$user_name)
    {
        $this->client_id            = $client_id;
        $this->client_secret        = $client_secret;
        $this->account_id           = $account_id;
        $this->user_name            = $user_name;

        $this->cache_access_token_name          = 'bdl_access_token' . '_' . $this->account_id;
        $this->cache_refresh_access_token_name  = 'bdl_refresh_access_token' . '_' . $this->account_id;

    }
    public function getToken()
    {
        $access_token = $this->getAccessTokenFromCache();

        if ($access_token)
        {
            return $access_token;
        }

        $refresh_token = $this->getRefreshTokenFromCache();


        if ($refresh_token) {
            if ($this->refreshToken($refresh_token)) {
                return $this->getAccessTokenFromCache();
            }
        } else {
            if ($this->accessToken()) {
                return $this->getAccessTokenFromCache();
            }
        }

        return 'error';
    }



    public function accessToken()
    {
        $method         = "oauth2/getToken";

        $fullurl        = $this->url . $method;

        $response_data = [
            'client_id'                     => $this->client_id,
            'client_secret'                 => $this->client_secret,
            'username'                      => $this->user_name,
            'accountId'                     => $this->account_id,
            'nonce'                         => uuid(),
            'timestamp'                     => date("Y-m-d H:i:s"),
            'language'                      => "zh-Cn"
        ];

        $curl = new Curl();

        $curl->setHeader("Content-Type", "application/json");

        $res = $curl->post($fullurl, json_encode($response_data));

        if($res->status == 1)
        {
            //加入缓存
            cache($this->cache_access_token_name, $res->data->access_token, $this->cache_access_token_time);
            //替换值即可
            cache($this->cache_refresh_access_token_name, $res->data->refresh_token,$this->cache_refresh_access_token_time);

            return true;
        }else{
            return 'token error';
        }
    }


    public function refreshToken($refresh_token)
    {
        $method         = "oauth2/refreshToken";

        $fullurl        = $this->url . $method;

        $response_data = [
            'client_id'                     => $this->client_id,
            'grant_type'                    => 'refresh_token',
            'refresh_token'                 => $refresh_token,
            'accountId'                     => $this->account_id,
            'nonce'                         => uuid(),
            'timestamp'                     => date("Y-m-d H:i:s",strtotime('-5 minutes')),
        ];

        $curl = new Curl();

        $curl->setHeader("Content-Type", "application/json");

        $res = $curl->post($fullurl, json_encode($response_data));

        if($res->status == 1)
        {
            //加入缓存
            cache($this->cache_access_token_name, $res->data->access_token, $this->cache_access_token_time);
            //替换值即可
            cache($this->cache_refresh_access_token_name, $res->data->refresh_token,$this->cache_refresh_access_token_time);

            return true;
        }else{
            return 'refresh token error';
        }

    }


    protected function getAccessTokenFromCache()
    {
        return cache($this->cache_access_token_name);
    }

    protected function getRefreshTokenFromCache()
    {
        return cache($this->cache_refresh_access_token_name);
    }


}